
#include "stdafx.h"
#include "QQController.h"
#include "QQManagerServer.h"
#include "../common/AuxFunc.h"
#include "TlHelp32.h"

using namespace std;

CQQController::CQQController(void)
{
	GetQQInstallPath();
}


CQQController::~CQQController(void)
{
}


// void CQQController::AttachQQ(QQData*)
// {
// 	DetachQQ();
// }
// 
// 
// void CQQController::DetachQQ()
// {
// 
// }


void CQQController::LoginQQ(QQNUM,std::string QQPassword)
{
	DWORD QQProcessId = OpenQQ();
	HWND hwndQQ;
	if(QQProcessId)
	{
		 hwndQQ = GetHwndByPID(QQProcessId);

		//FindWindowEx
	}
}


void CQQController::CloseQQ(QQNUM)
{


}


// void CQQController::InitQQ()
// {
// 
// }


bool CQQController::SendMsg(HWND hwnd,GOODS_ID goodid)
{
	if(!::IsWindow(hwnd))
		return false;
	return false;
	GoodsInfo* pGoodsInfo = CQQManagerServer::Ins()->GetGoodsInfo(goodid);
	ActivateQQWnd(hwnd);
	ImitateCtrlV(hwnd);
	ClickSendBtn(hwnd);
	return true;
}


// int CQQController::AllGroupSendMsg(GOODS_ID goodid)
// {
// 	HWND hwnd = _pCurQQData->FirstQQHWND();
// 	int nCount = 0;
// 	while (hwnd)
// 	{
// 		SendMsg(hwnd,goodid);
// 		hwnd = _pCurQQData->NextQQHWND();
// 		nCount++;
// 	}
// 	return nCount;
// }


// void CQQController::OpenQQGroupWnd()
// {
// 
// 
// }


void CQQController::CloseQQGroupWnd(string)
{

}


void CQQController::MoveQQWnd(HWND hwnd,POINT pt)
{

}


void CQQController::ActivateQQWnd(HWND hwnd)
{
	//WA_ACTIVE
	//WA_CLICKACTIVE
	::PostMessage(hwnd,WM_ACTIVATE,MAKEWPARAM(WA_ACTIVE,0),0);
}


void CQQController::ImitateCtrlC(HWND hwnd)
{
	INPUT input[3];
	memset(input, 0, 3 * sizeof(INPUT));

	input[0].type = INPUT_KEYBOARD;
	input[0].ki.wVk = VK_LCONTROL;
	SendInput(1, input, sizeof(INPUT));

	input[1].type = INPUT_KEYBOARD;
	input[1].ki.wVk = 'C';
	SendInput(1, input + 1,sizeof(INPUT));

	Sleep(300);
	input[2].type = INPUT_KEYBOARD;
	input[2].ki.wVk = VK_LCONTROL;
	input[2].ki.dwFlags = KEYEVENTF_KEYUP;
	SendInput(1, input + 2, sizeof(INPUT));

	ImitateKeyboardInput(input,3);
}


void CQQController::ImitateCtrlV(HWND hwnd)
{
	INPUT input[3];
	memset(input, 0, 3 * sizeof(INPUT));

	input[0].type = INPUT_KEYBOARD;
	input[0].ki.wVk = VK_LCONTROL;
	SendInput(1, input, sizeof(INPUT));

	input[1].type = INPUT_KEYBOARD;
	input[1].ki.wVk = 'V';
	SendInput(1, input + 1,sizeof(INPUT));

	Sleep(300);
	input[2].type = INPUT_KEYBOARD;
	input[2].ki.wVk = VK_LCONTROL;
	input[2].ki.dwFlags = KEYEVENTF_KEYUP;
	SendInput(1, input + 2, sizeof(INPUT));

	ImitateKeyboardInput(input,3);
}


void CQQController::ClickSendBtn(HWND hwnd)
{
	//GetInputState()
}


void CQQController::GetQQInstallPath()
{
	ATL::CRegKey regkey;
	TCHAR szValue [1024];
	DWORD dwCount = 512;

	wstring strKeyName = _T("SOFTWARE\\Wow6432Node\\Tencent\\PlatForm_Type_List\\1");
	if (regkey.Open (HKEY_LOCAL_MACHINE,strKeyName.c_str()) == ERROR_SUCCESS)
	{
		if (regkey.QueryStringValue(_T("TypePath"),szValue,&dwCount) == ERROR_SUCCESS)
		{
			USES_CONVERSION;
			_QQInstallPath = W2A(szValue);
		}
	}
}


DWORD CQQController::OpenQQ()
{
	BOOL bRet = false;
	STARTUPINFOA si = { sizeof(si) };
	PROCESS_INFORMATION pi = {0};

	if(!_QQInstallPath.empty())
	{
		CHAR szCommandLine[] = ("C:\\Windows\\System32\\calc.EXE");
		bRet = ::CreateProcessA(
			//
			(_QQInstallPath.c_str()  /*"D:\\Youdao\\Dict\\YodaoDict.exe"*/ /*"C:\\Windows\\System32\\calc.EXE"*/),
			szCommandLine,
			NULL,
			NULL,
			false,
			0,
			NULL,
			NULL,
			&si,
			&pi);

		if(bRet)
		{
			
		//	string strTitel = si.lpTitle;
		}
		else
		{
			int error = GetLastError();
		}
		//		BOOL bRet = CreateProcessA("D:\\QQ\\bin\\QQ.exe","",NULL,NULL,false,0,NULL,NULL,&si,&pi);
		
	}
	DWORD dw = WaitForInputIdle(pi.hProcess, INFINITE);
	if(dw == 0)
	{		int i = 2;
	}
	BITMAPINFO f;
	WTL::AtlLoadBitmapImage(,)
//	EnumProcesses(,);
	PROCESSENTRY32 de;
	DWORD fff = GetProcessId(pi.hProcess);
	return pi.dwProcessId;
}


void CQQController::_()
{

}


void CQQController::ImitateLButtonDown(HWND hwnd,POINT pt)
{
	::PostMessage(hwnd,WM_LBUTTONDOWN,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));
	::PostMessage(hwnd,WM_LBUTTONUP,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));
}


void CQQController::ImitateLButtonBLCLK(HWND hwnd,POINT pt)
{
	::PostMessage(hwnd,WM_LBUTTONDOWN,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));
	::PostMessage(hwnd,WM_LBUTTONUP,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));

	::PostMessage(hwnd,WM_LBUTTONDOWN,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));
	::PostMessage(hwnd,WM_LBUTTONUP,MAKEWPARAM(0,0),MAKELPARAM(pt.x,pt.y));
}


void CQQController::ImitateKeyboardInput(INPUT* input,int size)
{
	for (int i = 0;i < size ;++i)
	{
		SendInput(1, input++, sizeof(INPUT));
	}
}